﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using DataAccess;
using Entity;

namespace Web
{
    public partial class ProductList : System.Web.UI.Page
    {
        OrdersController orderscont;
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CategoryController categoryController=new CategoryController();
                IList<CategoryEntity> categoryEntities = categoryController.FindAllCategory();
                CategoryDropDownList.DataSource = categoryEntities;
                CategoryDropDownList.DataTextField = "Name";
                CategoryDropDownList.DataValueField = "Id";
                CategoryDropDownList.DataBind();
                CategoryDropDownList.Items.Insert(0,"--All--");

                GetProductEntities();

            }

            this.PreRender+=new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel); 
        }


        protected void ProductListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ProductListGridView.PageIndex = e.NewPageIndex;
           
            GetProductEntities();
        }

        protected void CategoryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetProductEntities();
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {

            GetProductEntities();

        }

        public void GetProductEntities()
        {
            string keyword = ProductNoTextBox.Text.Trim();
            int categoryId=0;
            if(CategoryDropDownList.SelectedIndex != 0)
            {
                categoryId = Convert.ToInt32(CategoryDropDownList.SelectedValue);
            }
            bool onlyShowProductInHand = ShowProductInHandCheckBox.Checked;

            IList<ProductEntity> productEntities = new List<ProductEntity>();
            ProductController productController = new ProductController();
            
            productEntities = productController.FindProductEntitiesByConditions(categoryId, keyword, onlyShowProductInHand);
            productEntities = productEntities.OrderByDescending(p => p.Id).ToList();

            ProductListGridView.DataSource = productEntities;
            ProductListGridView.DataBind();
            if (productEntities.Count <= 0)
            {
                // NofoundLabel.Text = "No match product!";
                Session["FlashMessage"] = "Notice: No Match Product!";
                Session["FlashMessageType"] = "notice";
            }
            else
            {
                NofoundLabel.Text = "";
            }
            
        }
    }
}